<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller
{
    protected $sesja= null;
    protected $log  = null;
    protected $elems= null;
    
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('Site_model','site');
        $this->sesja= $this->session->userdata('sesja');
        $this->log  = new Logevent();
        $this->elems= new Elements();
    }
    
    public function index()
    {
        if(isset($this->sesja['id']))
            redirect('users/show_panel');
        else
            redirect('news/list_news');
    }
    
    public function login()
    {
        if(!isset($this->sesja['id']))
        {
            $head['h1'] = 'Zaloguj się do systemu';
            $this->elems->load_view($this->sesja,'site/login','',$head);
        }
        else
            $this->index();
    }
    
    public function check_user()
    {
        if(!isset($this->sesja['id']))
        {
            $login = $this->input->post('name');
            $haslo = $this->input->post('pass');
            if(isset($_POST['login']) && !empty($login) && !empty($haslo))
            {                
                $haslo      = do_hash($haslo.$this->config->item('encryption_key'));
                $this->load->model('Users_model','users');
                $tab['post']= $this->users->check_user($login,$haslo);
                $this->log->log_event($this->sesja,$tab['post']['stan']);
                if($tab['post']['stan']==TRUE)
                {
                    if($tab['post']['wynik']['user_active'] != 0)
                    {
                        $id     = $tab['post']['wynik']['user_id'];
                        $konto  = $tab['post']['wynik']['user_type'];
                        $arr    = array(
                            'typ'   => $konto,
                            'id'    => $id, 
                            'login' => $login,
                            'editor'=> $tab['post']['wynik']['user_editor'],
                            'admin' => $tab['post']['wynik']['user_admin'],
                            'name'  => $tab['post']['wynik']['user_fname'].' '.$tab['post']['wynik']['user_lname']
                        );
                        $this->sesja = $this->session->set_userdata('sesja',$arr);
                        redirect('users/show_panel');
                    }
                    elseif($tab['post']['wynik']['user_active'] == 0)
                        $this->elems->load_alert($this->sesja,'error','Konto zostało usunięte lub zablokowane.');                  
                }
                else
                    $this->elems->load_alert($this->sesja,'error','Nie ma takiego użytkownika w systemie.<br/>'.$this->db->last_query());
            }
            else
                $this->elems->load_alert($this->sesja,'error','Błędne dane logowania');
        }
        else
            $this->index();
    }

    public function logout_user()
    {
        $this->session->unset_userdata('sesja');
        $this->session->sess_destroy();
        $this->sesja = null;            
        $this->index();
    }
    
    public function activate($hash)
    {
        $length = strlen($hash);
        if(!empty($hash) && $length==32)
        {
            $wynik = $this->site->check_hash($hash);
            if($wynik)
                $this->elems->load_alert($this->sesja,'success','Konto zostało aktywowane. Zaloguj się do systemu korzystając z podanych przez administratora danych.',anchor('site/login','Zaloguj się'));
            else
                $this->elems->load_alert($this->sesja,'error','Konto nie zostało aktywowane.');
        }
        else
         $this->elems->load_alert($this->sesja,'error','Nieprawidłowy adres URL');
    }
    
    public function options()
    {
        if(isset($this->sesja) && $this->sesja['admin']==1)
        {
            $head['h1'] = 'Ustawienia systemu';
            $this->load->model(array('League_model','Team_model'));
            $comps = $this->League_model->get_comps(2);
            if($comps['stan'])
            {
                $cont['comps'] = $comps['wynik'];
                $teams = $this->Team_model->get_select_teams(0);
                if($teams['stan'])
                {
                    $cont['teams'] = $teams['wynik'];
                    $foot['js'] = array('site/options_page.js');
                    $this->elems->load_view($this->sesja,'site/options_page',$cont,$head);
                }
            }
            else
                $this->elems->load_alert($this->sesja,'alert','Brak dostępnych rozgrywek',anchor('league/add_comp','Dodaj rozgrywki'));
        }
        else
            $this->elems->load_alert($this->sesja,'error','Nie masz uprawnień do korzystania z tej strony');
    }
    
    /*
     * zwraca ligi w formacie JSON do dynamicznego wyświetlania select listy na stronie
     * 
     * 
     */
    public function get_json_leagues($comp_id)
    {
        if(isset($this->sesja) && ($this->sesja['admin']==1 || $this->sesja['editor']) && is_numeric($comp_id))
        {
            $this->load->model('League_model');
            $json = $this->League_model->get_select_leagues($comp_id);
            if($json['stan'])
                return json_encode($json['wynik']);
            else
                return FALSE;
        }
        else
            return FALSE;
    }
}

/* End of file site.php */
/* Location: ./application/controllers/site.php */